﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmThreadingTimer : Form
    {
        public FrmThreadingTimer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 线程池timer
        /// </summary>
        System.Threading.Timer timer1 = null;
        System.Threading.Timer timer2 = null;
        private void FrmThreadingTimer_Load(object sender, EventArgs e)
        {
            //参数1：回调 ：重复执行操作 参数2： null 参数3: 设置timer的延迟执行的时间，参数4：时间间隔
            timer1 = new System.Threading.Timer(new System.Threading.TimerCallback(o =>
            {
                this.Invoke(new Action(() =>
                {
                    lblCurTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // 更新页面
                }));
            }), null, 100, 1000);
        }

        /// <summary>
        /// 20个座位
        /// </summary>
        int count = 20;
        int i = 1;

        /// <summary>
        /// 解决进入页面闪屏的问题，采用控件慢慢加载效果解决
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoad_Click(object sender, EventArgs e)
        {
            i = 1;
            flyPanels.Controls.Clear(); // 清空面板、
            // 实例化timer
            timer2 = new System.Threading.Timer(new System.Threading.TimerCallback(o => 
            {
            Action act = AddLabel;
                this.Invoke(act); // 执行委托
            }), null, 100, 200);
        }

        /// <summary>
        /// 加载座位
        /// </summary>
        private void AddLabel()
        {
            if(i<=count)
            {
                // 实例化座位
                Label lbl = new Label();
                lbl.Name = "lbl" + i.ToString("00");
                lbl.Text="座位 "+ i.ToString("00");
                lbl.Size = new Size(60, 30);
                lbl.TextAlign = ContentAlignment.MiddleCenter;
                if(lbl.Enabled)
                {
                    lbl.Click += Lbl_Click;
                }
            
                flyPanels.Controls.Add(lbl); // 添加座位
                i++;
            }
            else
            {
                timer2.Dispose();  // 释放timer
            }
        }

        /// <summary>
        /// 座位点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Lbl_Click(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            if(lbl.BackColor==Color.Red)
            {  // 取消选中
                //lbl.BackColor = SystemColors.Control; // 设置系统颜色
                //lbl.ForeColor = Color.Black;
                lbl.Enabled = false;
            }
            else
            {   // 选中设置座位颜色
                lbl.BackColor = Color.Red;
                lbl.ForeColor = Color.White;
            }
          
        }

        /// <summary>
        /// 窗体关闭前事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmThreadingTimer_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Dispose();
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmThreadingTimer_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }
    }
}
